from pythonds.linklist import UnorderedList, OrderedList


def test():
    sl = UnorderedList()
    print(sl.__dict__)
    import random

    for i in range(8):
        if random.randint(0, 1):
            sl.add(i)
            print("add", i)
        else:
            sl.append(i)
            print("append", i)

    sl.insert(8, 8)
    sl.insert(0, 9)
    sl.insert(5, 10)
    sl.delete(0)  # 删除头，9
    sl.delete(4)  # 删除中间的10
    sl.delete(8)  # 删除尾，10
    sl.delete(6)
    print(" ".join([str(v) for v in sl]))

    x = sl.delete(7)  # 异常移除
    x = sl.insert(9, 14)  # 异常插入

    print(" ".join([str(v) for v in sl]))

    id = 3
    va = sl[id]
    print("sl[{}] = {}".format(id, va))
    sl[0] = 10
    sl[1] += 10
    sl[8] = 18
    print(" ".join([str(v) for v in sl]))

    # 验证serach、pop
    print("-" * 15)
    sl2 = UnorderedList()
    for i in range(8):
        sl2.append(i + 1)
    print(" ".join([str(v) for v in sl2]))

    idx = sl2.index(0)
    print(idx, "<->", sl2[idx])
    idx = sl2.index(5)
    print(idx, "<->", sl2[idx])
    idx = sl2.index(7)
    print(idx, "<->", sl2[idx])
    idx = sl2.index(8)
    print(idx, "<->", sl2[idx])

    fn = sl2.search(3)
    if fn is not None:
        print(fn.data)
        fn.data = 14

    print("len:", len(sl2))
    maxm = len(sl2)
    for m in range(maxm):
        x = sl2.pop()
        print(x, "<-", " ".join([str(v) for v in sl2]))

    x = sl2.pop()
    print(x)

    # 验证有序链表
    print("-" * 15)
    sl3 = OrderedList(False)
    sl3.insert(12)
    sl3.insert(18)
    sl3.insert(8)
    sl3.insert(12)
    for i in range(6):
        sl3.insert(random.randint(10, 20))
    print(" ".join([str(v) for v in sl3]))

    # 验证删除
    print("-" * 15)
    sl4 = UnorderedList()
    for k in range(10):
        sl4.append(k)

    print(" ".join([str(v) for v in sl4]))
    sl4.remove(4)
    sl4.remove(0)
    sl4.remove(9)
    sl4.remove(10)
    cur = sl4.search(5)
    if cur is not None:
        cur.data = 15
    print(" ".join([str(v) for v in sl4]))

    # 验证有序表查找
    print("-" * 15)
    sl5 = OrderedList()
    for k in range(10):
        sl5.insert(2 * k)

    cur = sl5.search(6)
    if cur is not None:
        cur.data = 16
    print(" ".join([str(v) for v in sl5]))
